package com.ran.delegate;

import com.ran.common.MessageResult;
import com.ran.entity.DO.UserDO;
import com.ran.entity.POJO.Data;
import com.ran.server.UserService;
import com.ran.server.impl.UserServiceImpl;
import com.ran.servers.protobuf.SocketGameProtocol;
import com.ran.utils.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import net.jodah.expiringmap.ExpiringMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.management.RuntimeErrorException;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 委派模式总分发
 * Version:1.0.0
 * @author ran
 * date 2024/5/08 10:00
 */
@Component
public class DelegateDistribution extends Thread {
    private static Logger logger = LoggerFactory.getLogger(DelegateDistribution.class);

    public DelegateDistribution(){

    }

    //循环分配任务
    @Override
    public void run() {
        //将扫描到的对象加入到容器中
        ApplicationContext context = null;
        try {
            do {
                context = SpringUtil.getApplicationContext();
                if (context != null){break;}else {Thread.sleep(5000);}
            }while (context == null);
        }catch (Exception e){
            logger.info("-------DelegateDistribution类获取接口类时出现错误---------原因："+e);
            e.printStackTrace();
        }



        if (context!= null){
            while (true){
                try {
                    Data data = MessageQueue.messageQueue.take(); //没有数据一直处于阻塞状态
                    SocketGameProtocol.MainPack message = data.getPack();
                    ChannelHandlerContext ctx = data.getChannel();
                    // 处理用户相关
                    if (message.getRequestCode()== SocketGameProtocol.RequestCode.User){
                        SocketGameProtocol.ActionCode actionCode = message.getActionCode();
                        UserService userService = context.getBean(UserService.class);
                        UserDO user = new UserDO(message.getData().unpack(SocketGameProtocol.LoginPack.class).getUsername(),
                                message.getData().unpack(SocketGameProtocol.LoginPack.class).getUsername());
                        if (actionCode == SocketGameProtocol.ActionCode.Login) {
                            MessageResult loginRes = userService.login(user);
                            ctx.writeAndFlush(returnPack(loginRes.getMessage()));
                        } else if (actionCode == SocketGameProtocol.ActionCode.Logon) {
                            MessageResult logonRes = userService.logon(user);
                            ctx.writeAndFlush(returnPack(logonRes.getMessage()));
                        }
                    }
                    if(message.getRequestCode()== SocketGameProtocol.RequestCode.State){

                    }


                }catch (Exception e){
                    logger.error("DelegateDistribution中处理器出现错误:"+e.getCause()+"--"+e.getStackTrace()[0]);
                }
            }
        }
    }
    private SocketGameProtocol.MainPack returnPack(String code){
        return code.equals("success")?SocketGameProtocol.MainPack.newBuilder().setReturnCode(SocketGameProtocol.ReturnCode.Succeed).build()
                :SocketGameProtocol.MainPack.newBuilder().setReturnCode(SocketGameProtocol.ReturnCode.Fail).build();
    }


}
